package 链表.easy;

import 链表.middle.ShiJiu;

/**
 * 移除链表元素
 * @author Lw中
 * @date 2020/9/23 15:18
 */
public class ErLingSan {

    class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    /**
     * 使用哨兵节点
     * @param head
     * @param val
     * @return
     */
    public ListNode removeElements(ListNode head, int val) {
        /* 哨兵节点 */
        ListNode node1 = new ListNode(0);
        /* 哨兵节点的next节点指向头节点 */
        node1.next = head;
        /* pre节点是哨兵节点，curr是头节点 */
        ListNode pre = node1, curr = head;
        while (curr != null) {
            /* 如果当前节点值域等于目标值 */
            if (curr.val == val) {
                /* 当前节点的next赋值给哨兵节点的next */
                pre.next = curr.next;
            } else {
                /* 当前节点赋值给pre节点 */
                pre = curr;
            }
            curr = curr.next;
        }
        return node1.next;
    }

    public static void main(String[] args) {

    }

}
